<?php

/**
 * ecshop ERP v1.0
 * ----------------------------------------------------------------------------
 * http://www.phpally.com
 * Jacklee的博客 致力于php技术
 * ----------------------------------------------------------------------------
 * 作者: Jacklee
 * 邮箱: jack349392900@gmail.com
 * 创建时间: 2013-11-18
 * 最后修改时间: 2013-11-18
 */
 
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
require_once(dirname(__FILE__) . '/includes/lib/lib_erp_common.php');
require_once(dirname(__FILE__) . '/includes/lib/lib_erp_finance.php');
require_once(dirname(__FILE__) . '/includes/lib/lib_erp_supplier.php');
require_once(dirname(__FILE__) . '/includes/lib/lib_erp_goods_attr.php');

if ($_REQUEST['act'] == 'edit_payment')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	if(!admin_priv('erp_finance_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	$payment_id=intval($_REQUEST['payment_id']);
	
	if(!is_payment_exists($payment_id))
	{
		$result['error']=4;
		$result['message']=$_LANG['erp_payment_not_exist'];
		die($json->encode($result));
	}

	if(lock_payment($payment_id,'edit'))	
	{
		if(payment_act_record($payment_id,'modify'))
		{
			$result['error']=0;
			die($json->encode($result));
		}
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_payment_no_accessibility'];
		die($json->encode($result));		
	}
}

elseif ($_REQUEST['act'] == 'change_order')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	if(!admin_priv('erp_finance_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}

	$payment_id=intval($_REQUEST['payment_id']);	
	$order_id=intval($_REQUEST['order_id']);
	$pay_amount=intval($_REQUEST['pay_amount']);

	if(lock_payment($payment_id,'edit'))	
	{	
		if(payment_act_record($payment_id,'modify'))
		{	
			update_payment($payment_id,$order_id,$pay_amount);
			
			$payment_info=get_payment_info($payment_id);
			
			$result['pay_to']=$payment_info[0]['supplier_info'][0]['name'];
			$result['bill_date']=$payment_info[0]['bill_date'];
			$result['bill_amount']=$payment_info[0]['bill_amount'];
			$result['bill_total_payable']=$payment_info[0]['bill_total_payable'];
			$result['bill_paid_payable']=$payment_info[0]['bill_paid_payable'];
			$result['bill_payable_balance']=$payment_info[0]['bill_payable_balance'];
			$result['pay_amount']=$payment_info[0]['pay_amount'];
			
			$result['bill_no']=$payment_info[0]['order_info']['order_sn'];
			
			$result['error']=0;
			die($json->encode($result));
		}
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_payment_no_accessibility'];
		die($json->encode($result));		
	}		
}

elseif ($_REQUEST['act'] == 'change_bank_account')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	if(!admin_priv('erp_finance_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}

	if(empty($_REQUEST['bank_account_id']))
	{
		$result['error']=2;
		$result['message']=$_LANG['erp_wrong_parameter'];
		die($json->encode($result));
	}
	$payment_id=intval($_REQUEST['payment_id']);	
	$bank_account_id=intval($_REQUEST['bank_account_id']);
	
	if(lock_payment($payment_id,'edit'))	
	{
		if(payment_act_record($payment_id,'modify'))
		{
			$sql="update ".$GLOBALS['ecs']->table('erp_payment')." set account_id='".$bank_account_id."' where payment_id='".$payment_id."'";
			$GLOBALS['db']->query($sql);
			
			$account_info=get_bank_account_info($bank_account_id);
			
			$result['error']=0;
			$result['account_name']=$account_info['account_name'];
			die($json->encode($result));	
		}
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_payment_no_accessibility'];
		die($json->encode($result));		
	}						
}


elseif ($_REQUEST['act'] == 'change_payment_remark')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	if(!admin_priv('erp_finance_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}
	
	if(empty($_REQUEST['payment_id']))
	{
		$result['error']=2;
		$result['message']=$_LANG['erp_wrong_parameter'];
		die($json->encode($result));
	}

	$payment_id=intval($_REQUEST['payment_id']);	
	$payment_remark=trim($_REQUEST['payment_remark']);
	
	if(lock_payment($payment_id,'edit'))	
	{
		if(payment_act_record($payment_id,'modify'))
		{
			$sql="update ".$GLOBALS['ecs']->table('erp_payment')." set payment_remark='".$payment_remark."' where payment_id='".$payment_id."'";
			$GLOBALS['db']->query($sql);
			
			$result['error']=0;
			die($json->encode($result));	
		}
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_payment_no_accessibility'];
		die($json->encode($result));		
	}						
}

elseif ($_REQUEST['act'] == 'change_pay_amount')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	if(!admin_priv('erp_finance_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}

	$payment_id=intval($_REQUEST['payment_id']);	
	$pay_amount=floatval($_REQUEST['pay_amount']);
	
	if(lock_payment($payment_id,'edit'))	
	{
		if(payment_act_record($payment_id,'modify'))
		{
			$payment_info=get_payment_info($payment_id);
			$order_id=$payment_info[0]['bill_id'];
			
			$order_info=get_payable_order($order_id);
			
			update_payment($payment_id,$order_id,$pay_amount);
			
			$result['error']=0;
			die($json->encode($result));			
			
		}
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_payment_no_accessibility'];
		die($json->encode($result));		
	}						
}

elseif ($_REQUEST['act'] == 'delete_payment')
{
	include('../includes/cls_json.php');
	$json  = new JSON;
	
	if(!admin_priv('erp_finance_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}

	$payment_id=intval($_REQUEST['payment_id']);
	
	if(!is_payment_exists($payment_id))
	{
		$result['error']=4;
		$result['message']=$_LANG['erp_payment_not_exist'];
		die($json->encode($result));
	}

	if(lock_payment($payment_id,'delete'))	
	{
			//删除付款单
			if(delete_payment($payment_id) && delete_payment_details($payment_id))
			{
				$result['error']=0;
				$result['message']=$_LANG['erp_payment_delete_sucess'];
				die($json->encode($result));	
			}	
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_payment_no_accessibility'];
		die($json->encode($result));		
	}
}

elseif ($_REQUEST['act'] == 'post_to_approve')
{
	include('../includes/cls_json.php');
	$json  = new JSON;

	if(!admin_priv('erp_finance_manage', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}

	$payment_id=intval($_REQUEST['payment_id']);
	
 	//判断订单是否存在，是否已被删除
	if(!is_payment_exists($payment_id))
	{
		$result['error']=4;
		$result['message']=$_LANG['erp_payment_not_exist'];
		die($json->encode($result));
	}	
 
	if(lock_payment($payment_id,'post_to_approve'))	
	{
		if(payment_act_record($payment_id,'post'))
		{	
			$sql="update ".$GLOBALS['ecs']->table('erp_payment')." set ";
			$sql.="payment_status ='2' ";
			$sql.="where payment_id='".$payment_id."'";	
			
			if($GLOBALS['db']->query($sql))
			{
				$result['error']=0;
				$result['message']=$_LANG['erp_payment_post_success'];
				
				die($json->encode($result));		
			}
		}
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_payment_no_accessibility'];
		die($json->encode($result));		
	}	
		
}


elseif ($_REQUEST['act'] == 'approve_pass' || $_REQUEST['act'] == 'approve_reject')
{
	include('../includes/cls_json.php');
	$json  = new JSON;

	//检查是否有审核订单的权限
	if(!admin_priv('erp_finance_approve', '', false))
	{
		$result['error']=1;
		$result['message']=$_LANG['erp_no_permit'];
		die($json->encode($result));
	}

	$payment_id=intval($_REQUEST['payment_id']);
	$approve_remark=trim($_REQUEST['approve_remark']);
	$act=$_REQUEST['act'];
	
 	//判断订单是否存在，是否已被删除
	if(!is_payment_exists($payment_id))
	{
		$result['error']=4;
		$result['message']=$_LANG['erp_payment_not_exist'];
		die($json->encode($result));
	}	
 
	if(lock_payment($payment_id,'approve'))	
	{
		if(payment_act_record($payment_id,'approve'))
		{	
			if($act=='approve_pass')
			{
				if(approve_payment_pass($payment_id,$approve_remark))
				{
					$result['error']=0;
					$result['message']=$_LANG['erp_payment_approve_success'];
				
					die($json->encode($result));					
				}			
			}
			else if($act=='approve_reject')
			{
				if(approve_payment_reject($payment_id,$approve_remark))	
				{
					$result['error']=0;
					$result['message']=$_LANG['erp_payment_approve_success'];
				
					die($json->encode($result));						
				}			
			}
		}
	}
	else{
		$result['error']=-1;
		$result['message']=$_LANG['erp_payment_no_accessibility'];
		die($json->encode($result));		
	}	
}

?>